//景点表
const express=require('express')
const router=express.Router()
const pool=require('../utils/db')
const Response=require('../utils/Response')
//cyss_scenic
//查询所有
router.get('/scenic-list',(req,res)=>{
    let {page,pagesize} = req.query
    let startIndex= (page - 1) * 10
    let size= parseInt(pagesize)

    let sql ="select * from cyss_scenic limit ?,?"
    pool.query(sql,[startIndex,size],(error,result)=>{
        if(error) {
            res.send(Response(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
})
// 根据景点ID查询景点
router.post('/scenic/id', (req,res) => {
    let {scenicId} = req.body
    // console.log('ID查询景点', req.body);
    let sql = "select * from cyss_scenic where scenic_id = ?"
    pool.query(sql, [scenicId], (error,result) => {
        if(error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    } ) 
})
//名称模糊查询景点  --后台管理走的接口
router.post('/scenic-name',(req,res)=>{
    let {name} =req.body
    // console.log('景点查询接收参数',req.body);
    let sql="select * from cyss_scenic where scenic_name like ?"
    pool.query(sql,[`%${name}%`],(error,result)=>{
        if(error){
            res.send(Response(500,error))
            throw error
        } 
        // else if (result.length === 0) {
        //     res.send()
        // }
        res.send(Response.ok(result))
    })
})

//添加景点
router.post('/scenic-add',(req,res)=>{
    let {scenic_name,scenic_price,scenic_desc,scenic_img}=req.body
    // console.log('增加景点', req.body);
    let sql="insert into cyss_scenic (scenic_name,scenic_price,scenic_desc,scenic_img) values(?,?,?,?)"
    console.log('添加的景点参数',req.body);
    pool.query(sql,[scenic_name,scenic_price,scenic_desc,scenic_img],(error,result)=>{
        if(error){
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok())
    })
})

//修改景点
router.post('/scenic-edit',(req,res)=>{
    let {scenic_name,scenic_price,scenic_desc,scenic_img,scenic_id}=req.body
    // console.log('传递要修改的景点参数',req.body);
    let sql="update cyss_scenic set scenic_name=?,scenic_price=?,scenic_desc=?, scenic_img=? where scenic_id=?"
    pool.query(sql,[scenic_name,scenic_price,scenic_desc,scenic_img,scenic_id],(error,result)=>{
        console.log(sql);
        if(error){
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
})

//删除景点
router.post('/scenic-del',(req,res)=>{
    let {id}=req.body
    // console.log('删除景点', req.body);
    let sql="delete from cyss_scenic where scenic_id=?"
    pool.query(sql,[id],(error,result)=>{
        if(error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok())
    })
})
//导出路由对象
module.exports=router